#include "Enemy.h"
#include "../Graphic/GraphicManager.h"
#include "../Graphic/TextureManager.h"
#include "../Game/Level.h"
#include "Bullet.h"
#include <GL/glut.h>
#include <math.h>

//Update the enemy position
void cEnemy::Update(float lfTimeStep)
{
  //To control the speed of the enemy he can only be moved every 075 ms
  mfElapsedTime += fabs(lfTimeStep);
  mfElapsedTime2 += fabs(lfTimeStep);
  if (mfElapsedTime < 0.250) return;
  
  //Local variables that hold the next position
  unsigned int luiX = muiX;
  unsigned int luiY = muiY;
  
  //Get the player
  cObject * loPlayer = cLevel::Get().GetPlayer();
  
  float lfDistance = loPlayer->GetDistance(luiX, luiY);
  eOrientation leOrientation = loPlayer->GetOrientation(luiX, luiY);

  if (lfDistance > 10) return;
  
  //Try to move there
  switch (leOrientation)
  {
    case eO_South:
      luiY++;
      break;
    case eO_SouthWest:
      luiY++;
      luiX--;
      break;
    case eO_West:
      luiX--;
      break;
    case eO_NorthWest:
      luiY--;
      luiX--;
      break;
    case eO_North:
      luiY--;
      break;
   case eO_NorthEast:
      luiY--;
      luiX++;
      break;
    case eO_East:
      luiX++;
      break;
    case eO_SouthEast:
      luiY++;
      luiX++;
      break; 
    default:
      break;
  }
 
  miLookingAt = leOrientation;
  
  //If the enemy is near shoot
  if ((lfDistance > 5) && (mfElapsedTime2>0.500))
  {
    cObject *lcBullet = new cBullet(muiX, muiY, miLookingAt, "Bulletred.png", eEnemy, 2);
    std::stringstream lsNumber;
    lsNumber << miBulletNumber;
    lcBullet->SetName("Bullet"+macName+lsNumber.str());
    miBulletNumber++;
    cLevel::Get().InsertObject(lcBullet);
    mfElapsedTime2 = 0.0f;
    return;
  }

   if (lfDistance > 5) return;

  //Check walls
  cLevel &lLevel = cLevel::Get();
  if (!lLevel.IsWall( luiX, luiY ))
  {
    muiX = luiX;
    muiY = luiY;
    mfElapsedTime = 0;
  }
}

//Render the enemy
void cEnemy::Render()
{
 //Get game graphic properties
  int miTileWidth = cGraphicManager::Get().GetTileWidth();
  int miTileHeight = cGraphicManager::Get().GetTileHeight();
  unsigned int luiXScreenOffset = cGraphicManager::Get().GetXScreenOffset();
  unsigned int luiYScreenOffset = cGraphicManager::Get().GetYScreenOffset();
  unsigned int luiGameHeight = cGraphicManager::Get().GetGameHeight();
 
  
  cTexture lTexture = cTextureManager::Get().GetTexture("Data/enemy.png");
  int tex1=lTexture.GetID();
  glEnable(GL_TEXTURE_2D);
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  glBindTexture(GL_TEXTURE_2D, tex1);  
  glColor3f(1.0,1.0,1.0);

  float coordx_tile = 0.0f;
  float coordy_tile = 0.0f;

  switch(miLookingAt)
  {
  case eO_North:
    coordx_tile = 0.25f;
    coordy_tile = 0.0f;
    break;
  case eO_South:
    coordx_tile = 0.0f;
    coordy_tile = 0.0f;
    break;
  case eO_East:
    coordx_tile = 0.25f;
    coordy_tile = 0.5f;
    break;
  case eO_West:
    coordx_tile = 0.0f;
    coordy_tile = 0.5f;
    break;
  case eO_NorthWest:
    coordx_tile = 0.50f;
    coordy_tile = 0.5f;
    break;
  case eO_NorthEast:
    coordx_tile = 0.75f;
    coordy_tile = 0.5f;
    break;
  case eO_SouthWest:
    coordx_tile = 0.50f;
    coordy_tile = 0.0f;
    break;
  case eO_SouthEast:
    coordx_tile = 0.75f;
    coordy_tile = 0.0f;
    break;
  }
 
  float lfCteX = 0.375/2.0;
  float lfCteY = 0.375;

  glBegin(GL_QUADS);    
    glTexCoord2f(coordx_tile, coordy_tile+lfCteY);	
    glVertex2i(muiX*miTileWidth, muiY*miTileHeight);
	  glTexCoord2f(coordx_tile+lfCteX, coordy_tile+lfCteY);	
    glVertex2i((muiX+1)*miTileWidth , muiY*miTileHeight);
	  glTexCoord2f(coordx_tile+lfCteX, coordy_tile);	
    glVertex2i((muiX+1)*miTileWidth, (muiY+1)*miTileHeight);
	  glTexCoord2f(coordx_tile, coordy_tile);	
    glVertex2i(muiX*miTileWidth , (muiY+1)*miTileHeight);
  glEnd();  
}
